package cn.edu.jju.psytest.web;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.jju.psytest.model.util.EntityVoConverter;
import cn.edu.jju.psytest.model.vo.QuizVo;
import cn.edu.jju.psytest.service.QuizService;

@SuppressWarnings("restriction")
@Controller
@RequestMapping("/quizzes")
public class QuizController {

	private QuizService quizService;

	/**
	 * Ajax
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String getQizzes(ModelMap model) {
		model.addAttribute("quizzesVo",
				EntityVoConverter.getQuizzesVo(quizService.loadQuizzes()));
		return "quizzes";
	}

	@RequestMapping("/{quizId}")
	public String getQuiz(@PathVariable int quizId, ModelMap model) {
		QuizVo quizVo = EntityVoConverter.getQuizVo(quizService
				.loadQuiz(quizId));
		model.addAttribute("quizVo", quizVo);
		return "quiz";
	}

	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public String createQuiz() {
		quizService.createQuiz();
		return "创建成功！";
	}

	/**
	 * Getters And Setters
	 */
	public QuizService getQuizService() {
		return quizService;
	}

	@Resource
	public void setQuizService(QuizService quizService) {
		this.quizService = quizService;
	}
}
